package com.ejzblog.mall.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * Description：
 * </p>
 *
 * @author Mango
 * @version v1.0.0
 * @ProjectName: community-mall-master
 * @Package: com.ejzblog.mall.utils
 * @date 2021-07-13 16:02
 * @see com.ejzblog.mall.utils
 */
@SuppressWarnings("ALL")
@Slf4j
public class GeneratedUtils {

    /**
     * 锁对象，可以为任意对象
     */
    private static Object LOCK_OBJ = "lockerOrder";

    /**
     * 订单号生成计数器
     */
    private static long orderNumCount = 0L;

    /**
     * 每毫秒生成订单号数量最大值
     */
    private static int maxPerMSECSize = 1000;

    /**
     * 生成订单编号
     *
     * @return 订单号
     */
    public static synchronized String orderGenerated() {
        String orderCode = "";
        try {
            // 最终生成的订单号
            synchronized (LOCK_OBJ) {
                // 取系统当前时间作为订单号变量前半部分，精确到毫秒
                long nowLong = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
                // 计数器到最大值归零，可扩展更大，目前1毫秒处理峰值1000个，1秒100万
                if (orderNumCount >= maxPerMSECSize) {
                    orderNumCount = 0L;
                }
                // 组装订单号
                String countStr = maxPerMSECSize + orderNumCount + "";
                orderCode = nowLong + countStr.substring(1);
                orderNumCount++;
            }
            log.info("本次生成的订单Code为：{}", orderCode);
        } catch (Exception e) {
            log.error("生成订单号错误为：{}", e.getMessage());
            e.printStackTrace();
        }
        return orderCode;
    }

    /**
     * 生成随机验证码
     *
     * @param count
     * @return
     */
    public static synchronized String smsGeneratedCode(int count) {
        String code = "";
        try {
            synchronized (LOCK_OBJ) {
                code = String.valueOf((int) ((Math.random() * 9 + 1) * Math.pow(10, count - 1)));
            }
            log.info("本次生成的订单Code为：{}", code);
        } catch (Exception e) {
            log.error("生成短信验证码错误为：{}", e.getMessage());
            e.printStackTrace();
        }
        return code;
    }

    private static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};

    /**
     * 生成商品code
     *
     * @return {@link String}
     */
    public static String generateShortUuid() {
        StringBuffer sb = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            sb.append(chars[x % 0x3E]);
        }
        return sb.toString();
    }

}
